//
//  cglistViewController.m
//  BAOASystem
//
//  Created by michael on 2018/6/4.
//  Copyright © 2018年 czd. All rights reserved.
//

#import "cglistViewController.h"
#import "cglistTableViewCell.h"
#import "cgxxViewController.h"
@interface cglistViewController ()

@end

@implementation cglistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor],NSForegroundColorAttributeName, nil];
 
[self.navigationController.navigationBar setTitleTextAttributes:attributes];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithHexString:@"0a62ea"];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

    self.title = NSLocalizedString(@"采购信息", nil);
    self.view.backgroundColor = [UIColor colorWithHexString:@"f0eff5"];
      self.navigationController.navigationBar.hidden = NO;
    // Do any additional setup after loading the view.
      _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth,KScreenHeight-kTopHeight) style:UITableViewStylePlain];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableView.backgroundColor = [UIColor whiteColor];
_tableView.dataSource = self;
_tableView.delegate = self;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;

[self.view addSubview:_tableView];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//        [self loadData]; //加载网络数据
     UIBarButtonItem * rightItem  = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"加号_icon"] style:UIBarButtonItemStylePlain target:self action:@selector(createNewSMSAction)];

                     self.navigationItem.rightBarButtonItem = rightItem;
 __weak typeof(self) weakSelf = self;
[self.tableView addHeaderWithCallback:^{
        weakSelf.currentPage = 1;
        [weakSelf loadData]; //加载网络数据
    }];
    [self.tableView headerBeginRefreshing];

    [self.tableView addFooterWithCallback:^{
//        if ((self.currentPage - 1)*KPageSize < self.totalCounts ){
   if (weakSelf.lastPage==0 ){

//        if (weakSelf.isSearching) {
////     [self loadSearchMore];
//}
//     else {
     [weakSelf loadMore];
//     }
        }else{

            [weakSelf.tableView footerEndRefreshing];
            [weakSelf.view makeToast:NSLocalizedString(@"没有更多了", nil)];

        }

    }];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadData) name:@"refreshData" object:nil];
      UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"返回"]style:UIBarButtonItemStylePlain target:self action:@selector(buttonClick)];
 self.navigationItem.leftBarButtonItem = item1;

}
- (void)buttonClick{
[self.navigationController popViewControllerAnimated:YES];
}
- (void)createNewSMSAction{
cgxxViewController *vc = [[cgxxViewController alloc]init];
[self.navigationController pushViewController:vc animated:YES];
}
-(void)viewDidLayoutSubviews

{

    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {

        [self.tableView setSeparatorInset:UIEdgeInsetsZero];

    }

 

    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {

        [self.tableView setLayoutMargins:UIEdgeInsetsZero];

    }

}



-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{

    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {

        [cell setSeparatorInset:UIEdgeInsetsZero];

    }

 

    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {

        [cell setLayoutMargins:UIEdgeInsetsZero];

    }

 

}
-(void)loadData{
        self.currentPage = 1;

    [MBProgressHUD showHUDAddedTo:self.view animated:YES];

NSString *website = [[NSUserDefaults standardUserDefaults] objectForKey:@"website"];
NSString *userName = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];
NSString *uid = [[NSUserDefaults standardUserDefaults] objectForKey:@"uid"];

   NSString *language = [[NSUserDefaults standardUserDefaults] objectForKey:@"language"];
    NSString *bodyStr = [NSString stringWithFormat:@"<?xml version='1.0' encoding='UTF-8'?><message><header><userName>%@</userName><password>%@</password><website>%@</website><language>%@</language><type>%@</type></header><body><uid>%@</uid><pageNumber>%d</pageNumber><pageSize>20</pageSize></body></message>",userName,password,website,language,BAtype,uid,_currentPage];
//NSString *bodyStr = @"<?xml version='1.0' encoding='UTF-8'?><message><header><userName>chgy1234</userName><password>8c13b5750412d922b01b2da95d24f8b6</password><website>1</website><language>0</language><type>1</type></header><body><username>chgy1234</username><title></title><pdstate></pdstate><pageNumber>1</pageNumber><pageSize>20</pageSize></body></message>";
  bodyStr= [bodyStr URLEncodedString];
 NSArray * array = @[@{@"arg0":bodyStr}];
     NSString * path = [NSString stringWithFormat:@"%@%@",BAip,bDemandList];
  [HttpTools requestWithURL:path arrays:array method:@"demandList" success:^(id JSON) {
      [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
  NSLog(@"%@",JSON);
   if (self.currentPage ==1)
        {
            [self.dataArray removeAllObjects];
        }
                  if([[NSString stringWithFormat:@"%@",JSON[@"code"]] isEqualToString:@"200"]){
                    
        _dataArray =   [NSMutableArray array];
NSDictionary *dic= JSON[@"dmdpage"];
NSArray *array =dic[@"list"];

for (NSDictionary *dic in array ) {
    NSDictionary *dict = dic;
    [_dataArray addObject:dict];
    
}


_lastPage = [dic[@"lastPage"]intValue];
  self.totalCounts =[dic[@"totalRow"] intValue];
[_tableView reloadData];
    [self.tableView headerEndRefreshing];

//        [self.tableView footerEndRefreshing];
        self.currentPage++;
                  }else{
                  
                     [self.view makeToast:JSON[@"msg"]];

                  }
    } failure:^(NSError *error) {
            [self.tableView headerEndRefreshing];

//        [self.tableView footerEndRefreshing];
      [MBProgressHUD hideAllHUDsForView:self.view animated:YES];

    }];
}
- (void)loadMore{
// [MBProgressHUD showHUDAddedTo:self.view animated:YES];

NSString *website = [[NSUserDefaults standardUserDefaults] objectForKey:@"website"];
NSString *userName = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];
NSString *uid = [[NSUserDefaults standardUserDefaults] objectForKey:@"uid"];

   NSString *language = [[NSUserDefaults standardUserDefaults] objectForKey:@"language"];
    NSString *bodyStr = [NSString stringWithFormat:@"<?xml version='1.0' encoding='UTF-8'?><message><header><userName>%@</userName><password>%@</password><website>%@</website><language>%@</language><type>%@</type></header><body><uid>%@</uid><pageNumber>%d</pageNumber><pageSize>20</pageSize></body></message>",userName,password,website,language,BAtype,uid,_currentPage];
//NSString *bodyStr =  [NSString stringWithFormat:@"<?xml version='1.0' encoding='UTF-8'?><message><header><userName>chgy1234</userName><password>8c13b5750412d922b01b2da95d24f8b6</password><website>1</website><language>0</language><type>1</type></header><body><username>chgy1234</username><title></title><pdstate></pdstate><pageNumber>%d</pageNumber><pageSize>20</pageSize></body></message>",_currentPage];;

  bodyStr= [bodyStr URLEncodedString];
 NSArray * array = @[@{@"arg0":bodyStr}];
     NSString * path = [NSString stringWithFormat:@"%@%@",BAip,bDemandList];
  [HttpTools requestWithURL:path arrays:array method:@"demandList" success:^(id JSON) {
//      [MBProgressHUD hideHUDForView:self.view animated:YES];
  NSLog(@"%@",JSON);
   [self.view makeToast:JSON[@"msg"]];
                  if([[NSString stringWithFormat:@"%@",JSON[@"code"]] isEqualToString:@"200"]){
                      
        _dataArray =   [NSMutableArray array];
NSDictionary *dic = JSON[@"dmdpage"];
NSArray *array =dic[@"list"];

for (NSDictionary *dic in array ) {
    NSDictionary *dict = dic;
    [_dataArray addObject:dict];
    
}
//_dataArray = dic[@"list"];
_lastPage = [dic[@"lastPage"]intValue];
  self.totalCounts =[dic[@"totalRow"] intValue];
[_tableView reloadData];
        [self.tableView footerEndRefreshing];

        self.currentPage++;
                  }
    } failure:^(NSError *error) {
        [self.tableView footerEndRefreshing];
//      [MBProgressHUD hideHUDForView:self.view animated:YES];

    }];

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

  static NSString *CellIdentifier = @"getProductLis";

  cglistTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (cell==nil) {
        cell=[[cglistTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
 NSDictionary *dic = self.dataArray[indexPath.row];
    cell.dic = dic;
    return cell;



 }
 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

 
    return  110;

}
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 添加一个删除按钮
    UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:NSLocalizedString(@"删除", nil)handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        NSLog(@"点击了删除");
        NSDictionary *dic = _dataArray[indexPath.row];
        [self scaction:dic[@"id"]];
        }];
      deleteRowAction.backgroundColor = [UIColor colorWithHexString:@"ff5a2d"];

    // 删除一个置顶按钮
//    UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:NSLocalizedString(@"下架", nil)handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
//        NSLog(@"点击了下架");
//            NSDictionary *dic = _dataArray[indexPath.row];
//        [self xjaction:dic[@"id"]];
//        }];
//    topRowAction.backgroundColor = [UIColor colorWithHexString:@"ff8607"];
    
    // 添加一个更多按钮
    UITableViewRowAction *moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:NSLocalizedString(@"修改", nil)handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        NSLog(@"点击了修改");
          NSDictionary *dic = _dataArray[indexPath.row];
        [self xgaction:dic[@"id"]];
        
    }];
        moreRowAction.backgroundColor = [UIColor colorWithHexString:@"ffc208"];

//    moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    
    // 将设置好的按钮放到数组中返回
    return @[deleteRowAction, moreRowAction];
    
}
- (void)scaction:(NSString *)idstring{
NSString *website = [[NSUserDefaults standardUserDefaults] objectForKey:@"website"];
NSString *userName = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];

   NSString *language = [[NSUserDefaults standardUserDefaults] objectForKey:@"language"];
NSString *bodyStr = [NSString stringWithFormat:@"<?xml version='1.0' encoding='UTF-8'?><message><header><userName>%@</userName><password>%@</password><website>%@</website><language>%@</language><type>%@</type></header><body><id>%@</id></body></message>",userName,password,website,language,BAtype,idstring];
 bodyStr= [bodyStr URLEncodedString];
 NSArray * array = @[@{@"arg0":bodyStr}];
     NSString * path = [NSString stringWithFormat:@"%@%@",BAip,bDemandList];
 [HttpTools requestWithURL:path arrays:array method:@"deleteDemand" success:^(id JSON) {
   NSLog(@"%@",JSON);

    [self.view makeToast:JSON[@"msg"]];
//[_tableView reloadData];
[self loadData];
 } failure:^(NSError *error) {

    }];

}
- (void)xgaction:(NSString *)idstring{
cgxxViewController *vc = [[cgxxViewController alloc]init];
vc.ipid = idstring;
vc.type = @"xiuggai";
vc.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:vc animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
